package com.iris.netty.review;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author victor
 */
public class BioServer {

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8889);
        ExecutorService executorService = Executors.newCachedThreadPool();
        while(true){
            final Socket socket = serverSocket.accept();
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    InputStream inputStream = null;
                    try {
                        inputStream = socket.getInputStream();
                        byte[] bytes = new byte[128];
                        while(true){
                            //读取数据（阻塞）
                            int read = inputStream.read(bytes);
                            if(read != -1){
                                System.out.println(new String(bytes, 0, read));
                            }else{
                                break;
                            }
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        try {
                            socket.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });

        }
    }
}
